from http import HTTPStatus

from flask import Blueprint, jsonify, request
from service import parkings as service

bp = Blueprint("parkings", __name__, url_prefix="/parkings")


@bp.route("/", methods=["POST"])
def create_parking_route():
    """Создать нового паркинга

    Examples:
        curl -X POST -H "Content-Type: application/json" \
        --data '{"address": "Московский пр., 131", "count_places": "300", "count_available_places": "300"}' \
             http://127.0.0.1:8000/parkings/
    """
    address = request.json.get("address", None)
    opened = request.json.get("opened", True)
    count_places = request.json.get("count_places", None)
    count_available_places = request.json.get("count_available_places", None)

    if address is None or count_places is None or count_available_places is None:
        return (
            jsonify(
                {
                    "error": "address, count_places, count_available_places fields are required"
                }
            ),
            HTTPStatus.BAD_REQUEST,
        )

    parking = service.create_parking(
        address, opened, count_places, count_available_places
    )
    return jsonify(parking), HTTPStatus.CREATED
